import { GameObject } from "../../engine";
import { System } from "./System";

export class GameLifeCycleSystem extends System {

    onStart(): void {
        function visit(gameObject: GameObject) {
            for (const behaviour of gameObject.behaviours) {
                behaviour.onStart();
            }
            for (const child of gameObject.children) {
                visit(child);
            }
        }
        visit(this.rootGameObject)
    }

    onTick(duringTime: number): void {
        function visit(gameObject: GameObject) {
            for (const behaviour of gameObject.behaviours) {
                behaviour.onTick(duringTime);
            }
            for (const child of gameObject.children) {
                visit(child);
            }
        }
        visit(this.rootGameObject)
    }

    onUpdate(): void {
        function visit(gameObject: GameObject) {
            for (const behaviour of gameObject.behaviours) {
                behaviour.onUpdate();
            }
            for (const child of gameObject.children) {
                visit(child);
            }
        }
        visit(this.rootGameObject)
    }

    onEnd(): void {
        function visit(gameObject: GameObject) {
            for (const behaviour of gameObject.behaviours) {
                behaviour.onEnd();
            }
            for (const child of gameObject.children) {
                visit(child);
            }
        }
        visit(this.rootGameObject)
    }
}